package controllers;

import de.puc.defencetower.MapFactory;
import de.puc.defencetower.controller.action.BuildTowerAction;
import de.puc.defencetower.controller.action.UpgradeTowerAction;
import de.puc.defencetower.controller.exception.GameOverException;
import de.puc.defencetower.controller.exception.TowerBuildException;
import de.puc.defencetower.controller.exception.TowerUpgradeException;
import de.puc.defencetower.model.*;
import de.puc.defencetower.view.gui.GUIWindow;
import de.puc.defencetower.view.tui.TUIWindow;
import de.puc.defencetower.view.tui.panel.PanelWindow;
import models.GameConnection;
import models.WUIFPSTimer;
import models.connection.GameException;
import models.connection.GameInitialisation;
import models.connection.GameUpdate;
import views.html.mapelements.*;

import static play.mvc.Controller.session;

/**
 * Created with IntelliJ IDEA.
 * User: chlemke
 * Date: 26.12.13
 * Time: 18:44
 * it have the timer
 * it have the Game Connection(s)
 * it have the gameConnection to the puc Controller
 */
public class GameController {

    public static Map map;
    private de.puc.defencetower.controller.impl.GameController pucGameController;
    public WUIFPSTimer wuiTimer;

    //TODO maybe as a map!
    GameConnection gameConnection = null;
    public String mapSize;

    public GameController(String mapSize){
        this.mapSize = mapSize;

        initNewGameConnection(mapSize);
    }

    // TODO think about more connections!
    public void initNewGameConnection(final String mapSize){
        try {
            gameConnection = new GameConnection(this, mapSize);
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }


    }

    // TODO think about more connections!
    public GameConnection getGameConnection(){
        return gameConnection;
    }

    // Wait 4 onready of the WebSocket
    public void initGame(String mapSize){
        // Map erzeugen
        createMap(mapSize);

        // GameControler starten
        pucGameController = new de.puc.defencetower.controller.impl.GameController(map);
        pucGameController.start();


        // TODO!!!!
        // tui
        //new TUIWindow(pucGameController, map);
        //new PanelWindow(pucGameController,map);

        // Anzeige GUI
        //new GUIWindow(pucGameController, map);
        new PanelWindow(pucGameController,map);

        GameInitialisation gameInit = new GameInitialisation(map);
        gameConnection.send(gameInit.getJson());

        final int[] c = {0};

        wuiTimer = new WUIFPSTimer(new WUIFPSTimer.iRefreshPrinter() {
            @Override
            public void print() {

                // Map  changes
                GameUpdate gu = new GameUpdate(map) ;
                //System.out.println(c[0]);
                c[0]++;

                gameConnection.send(gu.getJson());
            }

        });
        wuiTimer.start();
    }

    private void createMap(String mapSize){
        switch (mapSize){
            case "mini":
                map = MapFactory.createMiniMap();
                break;
            case "small":
                map = MapFactory.createSmallMap();
                break;
            case "middle":
                map = MapFactory.createMiddleMap();
                break;
            case "big":
                map = MapFactory.createBigMap();
                break;
        }
    }

    public void nextWave() {
        try {
            pucGameController.spwanNextWave();
        } catch (GameOverException e) {
            gameConnection.send(new GameException(e).getJson());

            pucGameController.gameOver();
        }
    }

    public void updateTower(int posx, int posy) {
        try{
            pucGameController.upgradeTower(new UpgradeTowerAction(posx, posy, pucGameController, map));
        } catch ( TowerUpgradeException e){
            gameConnection.send(new GameException(e).getJson());
        }

    }

    public void buildTower(int posx, int posy, int towersize) {

        try{
            pucGameController.buildTower(new BuildTowerAction(posx, posy, towersize, pucGameController));
        } catch(TowerBuildException e){
            gameConnection.send(new GameException(e).getJson());
        }
    }


    public void cleanUp() {
        System.out.println("cleanUp");
        if(wuiTimer != null && wuiTimer.timerTask != null){
            wuiTimer.timerTask.cancel();
            wuiTimer.timerTask = null;
        }
        if(wuiTimer != null){
            wuiTimer.cancel();
            wuiTimer = null;
        }

    }
}
